package com.knowif.Controller;


import com.alibaba.fastjson.JSONObject;
import com.knowif.pojo.MessageChat;
import com.knowif.pojo.UserChat;
import com.knowif.service.UserChatService;
import com.knowif.system.model.RestBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;


@RequestMapping("/wechat/")
@RestController
public class ChatController {
    @Autowired
    private UserChatService userChatService;
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @PostMapping("addChat")
    public RestBean<?> addChat(@RequestBody MessageChat messageChat){
        return userChatService.addUserChat(messageChat);
    }

    @PostMapping("uploadImage")
    public RestBean<?> uploadImage(@RequestParam("image") MultipartFile image){
        return userChatService.updateImage(image);
    }

    @GetMapping("getUserChatListByType")
    public RestBean<?> getUserChatListByType(int orderId,int userId){
        return userChatService.selectUserChatListByType(userId,orderId);
    }

    @GetMapping("getUserChatList")
    public RestBean<?> getUserChatList(int orderId){
        return userChatService.selectUserChatList(orderId);
    }

    @GetMapping("testss")
    public String testss(){
        UserChat userChat = new UserChat();
        userChat.setContent("这个是正文");
        userChat.setCreateTime(new Date());
        userChat.setParentId(1);
        userChat.setUserId(40);
        userChat.setUserOrderId(2);
        simpMessagingTemplate.convertAndSendToUser("test","/oop", JSONObject.toJSONString(userChat));
        return "ok";
    }

}
